package org.hot100_6.leetcode128;

import java.util.HashMap;
import java.util.HashSet;

public class Solution2 {
    public int longestConsecutive(int[] nums) {
        HashSet<Integer> hashSet = new HashSet<>();

        for (int num : nums) {
            hashSet.add(num);
        }

        int maxLength = 0;

        int currentLength = 0;

        for (int num : hashSet) {
            currentLength = 1;
            if (!hashSet.contains(num - 1)){
                int temp = num+1;
                while (hashSet.contains(temp)){
                    currentLength++;
                    temp++;
                }
                maxLength = Math.max(maxLength,currentLength);
            }
        }

        return maxLength;
    }
}
